﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using PoolGame2.Game;

namespace PoolGame2.Controls
{
    class XBoxGameControls : GameControls
    {

        private Dictionary<Buttons, ControlActions> keyDownControlMappings = new Dictionary<Buttons, ControlActions>();
        private Dictionary<Buttons, ControlActions> keyPressedControlMappings = new Dictionary<Buttons, ControlActions>();
        private bool leftButtonDown = false;

        public XBoxGameControls(PoolGame game) : base(game)
        {

            keyPressedControlMappings.Add(Buttons.RightStick, ControlActions.CAMERA_CYCLE_UP);
            //keyPressedControlMappings.Add(Keys.PageDown, ControlActions.CAMERA_CYCLE_DOWN);
            keyPressedControlMappings.Add(Buttons.LeftStick, ControlActions.SWITCH_TRACKING);

            keyDownControlMappings.Add(Buttons.LeftThumbstickUp, ControlActions.MOVE_FORWARD);
            keyDownControlMappings.Add(Buttons.LeftThumbstickDown, ControlActions.MOVE_BACK);
            keyDownControlMappings.Add(Buttons.LeftThumbstickLeft, ControlActions.STRAFE_LEFT);
            keyDownControlMappings.Add(Buttons.LeftThumbstickRight, ControlActions.STRAFE_RIGHT);

            keyDownControlMappings.Add(Buttons.DPadUp, ControlActions.DPAD_UP);
            keyDownControlMappings.Add(Buttons.DPadDown, ControlActions.DPAD_DOWN);
            keyDownControlMappings.Add(Buttons.DPadLeft, ControlActions.DPAD_LEFT);
            keyDownControlMappings.Add(Buttons.DPadRight, ControlActions.DPAD_RIGHT);

            keyDownControlMappings.Add(Buttons.Back, ControlActions.REPLAY_YES);
        }

        public override void PollController(GameTime gameTime)
        {
            List<GameControlAction> actions = new List<GameControlAction>();

            HandleKeys(actions);
            HandleAiming(actions);

            if (actions.Count > 0)
            {
                this.NotifyListeners(actions);
            }
        }

        private void HandleKeys(List<GameControlAction> actions)
        {
            foreach (System.Collections.Generic.KeyValuePair<Buttons, ControlActions> pair in keyPressedControlMappings)
            {
                if (IsKeyPressed(pair.Key))
                {
                    GameControlAction action = new GameControlAction(pair.Value);
                    actions.Add(action);
                }
            }
            foreach (System.Collections.Generic.KeyValuePair<Buttons, ControlActions> pair in keyDownControlMappings)
            {
                if (IsKeyDown(pair.Key))
                {
                    GameControlAction action = new GameControlAction(pair.Value);
                    actions.Add(action);
                }
            }

        }

        private void HandleAiming(List<GameControlAction> actions)
        {
            if (IsKeyPressed(Buttons.A))
            {
                actions.Add(new GameControlAction(ControlActions.START_SHOT));
                leftButtonDown = true;
            }
            if (IsKeyUp(Buttons.A) && leftButtonDown)
            {
                actions.Add(new GameControlAction(ControlActions.END_SHOT));
                leftButtonDown = false;
            }

            {
                Vector2 newPosition = new Vector2();
                if( currentGamePadState.IsButtonDown(Buttons.DPadLeft ) )
                    newPosition.X += 1f;
                if (currentGamePadState.IsButtonDown(Buttons.DPadRight))
                    newPosition.X -= 1f;

                if (newPosition.Length() > 0)
                {
                    GameControlAction action;
                    action = new GameControlAction(ControlActions.CUE_MOVEMENT);
                    action.AddProperty(GameConstants.X_CHANGE, newPosition.X);
                    action.AddProperty(GameConstants.Y_CHANGE, newPosition.Y);
                    actions.Add(action);
                }
            }
        }
    }
}
